package com.example.demo.controller;

import com.example.demo.entity.ResultMsg;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * @Author: zcb
 * @Description:
 * @Date: 2021/9/27
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;


    @GetMapping("firstUser")
    public ResultMsg firstUser(@RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "companyId", required = false) Byte companyId) {
        return userService.firstUser(name, companyId);
    }

    @GetMapping("getOne")
    public User getOne(@RequestParam("name") String name) {
        return userService.getOne(name);
    }

    @PostMapping("insertUser")
    public ResultMsg insertUser(@RequestParam("name") String name,
                                @RequestParam("companyId") Byte companyId) {
        try {
            return userService.insertUser(name, companyId);
        }catch (Exception e) {
            log.error("新增用户失败：{}", e.getMessage());
            return ResultMsg.systemError();
        }
    }

    @GetMapping("userOrder")
    public ResultMsg userOrder(@RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "companyId", required = false) Byte companyId) {
        try {
            return userService.userOrder(name, companyId);
        }catch (Exception e) {
            log.error("用户订单查询失败：{}", e.getMessage());
            return ResultMsg.systemError();
        }
    }

    @PostMapping("updateUserOrderAmount")
    public ResultMsg updateUserOrderAmount(@RequestParam("userId") Long userId,
                                           @RequestParam("amount") int amount,
                                           @RequestParam("companyId") Byte companyId) {
        return userService.updateUserOrderAmount(userId, amount, companyId);
    }
}
